package com.crm.customer.service;

import com.crm.common.mapper.ActivityMapper;
import com.crm.common.pojo.Activity;
import com.crm.global.exception.CrmException;
import com.crm.global.exception.ExceptionEnum;
import com.crm.id.increment.IdWorker;
import com.crm.mybatis.separation.Read;
import com.crm.mybatis.separation.Write;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class ActivityService {

    @Autowired
    private ActivityMapper activityMapper;

    @Read
    public List<Activity> findAll(Long customerId) {
        if (customerId == null) {
            throw new CrmException(ExceptionEnum.PARAM_IS_NULL);
        }
        Example example = new Example(Activity.class);
        example.createCriteria().andEqualTo("customerId",customerId);
        return activityMapper.selectByExample(example);
    }

    @Write
    public void saveOrUpdate(Activity activity) {
        if (activity == null) {
            throw new CrmException(ExceptionEnum.PARAM_IS_NULL);
        }
        if (activity.getId() == null) {
            activity.setId(new IdWorker().nextId());
            activityMapper.insert(activity);
        } else {
            activityMapper.updateByPrimaryKey(activity);
        }
    }

    @Write
    public void deleteById(Long id) {
        if (id == null) {
            throw new CrmException(ExceptionEnum.PARAM_IS_NULL);
        }
        activityMapper.deleteByPrimaryKey(id);
    }

    @Write
    public void deleteByCustomerId(Long customerId) {
        if (customerId == null) {
            throw new CrmException(ExceptionEnum.PARAM_IS_NULL);
        }
        Example example = new Example(Activity.class);
        example.createCriteria().andEqualTo("customerId",customerId);
        activityMapper.deleteByExample(example);
    }

}
